#pragma once


namespace wjb
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
		{}

		~vector()
		{
			if (_start)
			{
				delete[] _start;
				_start = _finish = _endofstoarge = nullptr;
			}
		}

		template<class inputiterator>
		vector(inputiterator first, inputiterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}

		vector(const vector<T>& v)
		{
			_start = new T[v.capacity()];
			for (size_t i = 0; i < v.size(); ++i)
			{
				_start[i] = v[i];
			}
			_finish = _start + v.size();
			_endofstoarge = _start + v.capacity();
		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

		T& operator[](size_t pos)
		{
			assert(pos >= 0 && pos < size());
			return _start[pos];
		}

		const T& operator[](size_t pos)const
		{
			assert(pos >= 0 && pos < size());
			return _start[pos];
		}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_endofstoarge, v._endofstoarge);

		}

		void resize(size_t n, const T& val = T())
		{
			if (n > size())
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = val;
					++_finish;
				}
			}
			else
			{
				_finish = _start + n;
			}
		}

		size_t size()const
		{
			return _finish - _start;
		}

		size_t capacity()const
		{
			return _endofstoarge - _start;
		}

		void push_back(const T& val)
		{
			if (size() == capacity())
			{
				reserve(size() == 0 ? 4 : capacity() * 2);
			}
			*_finish = val;
			++_finish;
		}

		void pop_back()
		{
			--_finish;
		}



		void reserve(size_t n)
		{
			if (n > size())
			{
				size_t sz = size();
				T* tmp = new T[n];
				if (_start)
				{
					for (size_t i = 0; i < sz; ++i)
					{
						tmp[i] = _start[i];
					}
					delete[] _start;
				}
				_start = tmp;
				_finish = _start + sz;
				_endofstoarge = _start + n;
			}
		}

		iterator insert(iterator pos,const T& val)
		{
			assert(pos >= _start && pos <= _finish);
			size_t len = pos - _start;
			if (size() == capacity())
			{
				reserve(size() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}
			iterator end = _finish - 1;
			while (end >= pos)
			{
				*(end + 1) = *end;
				--end;
			}
			*pos = val;
			++_finish;
			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);
			iterator it = pos + 1;
			while (it < end())
			{
				*(it - 1) = *it;
				++it;
			}
			--_finish;
			return pos;
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator cbegin()const
		{
			return _start;
		}

		const_iterator cend()const
		{
			return _finish;
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstoarge = nullptr;
	};

	void vector_test1()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		v.pop_back();
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

	}
	void vector_test2()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		v.insert(v.begin(),69999);
		v.insert(v.begin()+3, 69999);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		v.erase(v.begin());
		v.erase(v.begin() + 3);

		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void vector_test3()
	{
		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(3);
		v.push_back(4);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		v.resize(3);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		v.resize(10);
		for (auto e : v)
		{
			cout << e << " ";
		}
		cout << endl;

		vector<int> v2(v);
		for (auto e : v2)
		{
			cout << e << " ";
		}
		cout << endl;

		vector<int> v3;
		v3 = v2;
		for (auto e : v3)
		{
			cout << e << " ";
		}
		cout << endl;

		vector<string> s;
		s.push_back("fdasfdsagfsdgsdfg");
		s.push_back("dasfgdsagfdag");
		s.push_back("afadgfdasfdsagfsjhgfjdgsdfg");
		s.push_back("jghjfdasfdsagfskjhglkjhgldgsdfg");

		int a[] = { 1,3,5,6,3,6,7 };
		for (auto e : a)
		{
			cout << e << " ";
		}
		cout << endl;
		vector<string> v4(s.begin(), s.end());
		vector<int> v5(a, a + 7);
		for (auto e : v4)
		{
			cout << e << " ";
		}
		cout << endl;
		for (auto e : v5)
		{
			cout << e << " ";
		}
		cout << endl;

	}


}

